﻿using NCP.Core;
using NCP.Model;
using NCP.Service;
using NCP.Util;
using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using WebApp.Filters;

namespace WebApp.Controllers
{
    [Authen]
    public class MemberController : Controller
    {
        private MemberService _memberService;
        public MemberController(MemberService memberService)
        {
            _memberService = memberService;
        }

        public ActionResult UpdatePwdView()
        {
            return View();
        }

        public ActionResult GetList()
        {
            Expressionable<Member> exp = new Expressionable<Member>();
            int pageIndex = 1;
            int pageSize = 20;
            if (!string.IsNullOrEmpty(Request.Form["page"]))
            {
                pageIndex = Convert.ToInt32(Request.Form["page"]);
            }
            if (!string.IsNullOrEmpty(Request.Form["limit"]))
            {
                pageSize = Convert.ToInt32(Request.Form["limit"]);
            }
            exp.AndIF(!string.IsNullOrEmpty(Request["Name"]), t => t.Name.Contains(Request["Name"]));
            exp.AndIF(!string.IsNullOrEmpty(Request["DeptCode"]), t => t.DeptCode== Request["DeptCode"]);
            PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
            List<Member> dataList = _memberService.GetPageList(exp, pageModel);
            dataList.ForEach(t=> {
                CommonUtil.BuildText<Member>(t);
            });
            LayData ht = new LayData("0", "获取成功", dataList, pageModel.TotalCount);

            return Content(JsonConvert.SerializeObject(ht));
        }

        public ActionResult UpdatePassword()
        {
            string password1 = Request.Form["password1"] + "";
            string password2 = Request.Form["password2"] + "";
            LayData ht = new LayData("0", "修改成功");
            if (string.IsNullOrWhiteSpace(password1) || string.IsNullOrWhiteSpace(password2))
            {
                ht = new LayData("1", "密码不能为空！");
                return Content(JsonConvert.SerializeObject(ht));
            }
            if (password1 != password2)
            {
                ht = new LayData("1", "两次密码输入不一致，请重新输入！");
                return Content(JsonConvert.SerializeObject(ht));
            }
            //密码加密
            string newpas = HashEncrypt.Md5Hash(password1.Trim());
            int et = _memberService.UpdatePwd(newpas,MemberUtil.CurrentMember.Id);
            return Content(JsonConvert.SerializeObject(ht));
        }

        public ActionResult ResetPwd()
        {
            int id = Request.Form["Id"].ObjToInt();
           
            //密码加密
            string pwd = ConfigurationManager.AppSettings["pwd"].ToString();
            string newpas = HashEncrypt.Md5Hash(pwd);
            int et = _memberService.UpdatePwd(newpas, id);
            LayData ht = new LayData("0", "修改成功,修改后密码："+pwd);
            return Content(JsonConvert.SerializeObject(ht));
        }
    }
}